package com.yuan.yblog.portal.pojo.VO;

import com.yuan.yblog.crud.entity.Category;
import com.yuan.yblog.crud.entity.Post;
import com.yuan.yblog.crud.entity.User;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serial;
import java.io.Serializable;
import java.util.Date;


/*
    实体Post的子类，
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Schema(description = "文章列表DTO, 用于展示在博客列表的表项")
public class PostItemVO extends Post implements Serializable {

    @Schema(description = "文章作者名")
    private String userName;

    @Schema(description = "文章作者头像")
    private String avatar;

    @Schema(description = "分类名")
    private String categoryName;

    @Serial
    private static final long serialVersionUID = 1L;

    public static PostItemVO of(Post post, User user, Category category){
        PostItemVO postItemVO = new PostItemVO();
        if (post != null) {
            postItemVO.setId(post.getId());
            postItemVO.setTitle(post.getTitle());
            postItemVO.setCommentCount(post.getCommentCount());
            postItemVO.setLevel(post.getLevel());
            postItemVO.setRecommend(post.getRecommend());
            postItemVO.setCreated(post.getCreated());
        }
        if (user != null) {
            postItemVO.setUserId(user.getId());
            postItemVO.setUserName(user.getUsername());
            postItemVO.setAvatar(user.getAvatar());
        }
        if (category != null) {
            postItemVO.setCategoryId(category.getId());
            postItemVO.setCategoryName(category.getName());
        }
        return postItemVO;
    }
}
